package ch.fhnw.qualiscan.core.helpers;

import java.io.Serializable;

import ch.fhnw.qualiscan.application.models.UserModel;
import ch.fhnw.qualiscan.core.db.pojos.User;
import eu.livotov.tpt.TPTApplication;

/**
 * Provides functionality to get and set a user pojo from and into the user session.
 * 
 * @author Kevin Mettenberger
 *
 */

@SuppressWarnings("serial")
public class UserSessionHelper implements Serializable {
	
	
	/**
	 * Stores a UserSession pojo to the user session.
	 * @param usersession The UserSession pojo to be stored.
	 */
	public void setUserSession(UserSession usersession) {
		TPTApplication.getCurrentApplication().setUser(usersession);
	}
	
	/**
	 * Returns a UserSession pojo if there's  one stored in the user session. Else null.
	 * @return Returns a UserSession pojo.
	 */
	public UserSession getUserSession() {
		if(isUserSessionSet()) {
			return (UserSession) TPTApplication.getCurrentApplication().getUser();
		}
		return null;
	}
	
	/**
	 * 
	 * @return Returns true, if a user session is set.
	 */
	public boolean isUserSessionSet() {
		if(TPTApplication.getCurrentApplication().getUser() != null) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * Sets the user session back to null (logged out).
	 */
	public void deleteUserSession() {
		TPTApplication.getCurrentApplication().setUser(null);
	}
	
	public User getUser() {
		UserModel userModel = new UserModel();

		UserSessionHelper sessionHelper = new UserSessionHelper();
		
		if(sessionHelper.isUserSessionSet()) {
			User user = userModel.getUserById(sessionHelper.getUserSession().getId());
			return user;
		}
		else {
			return null;
		}
	}
}
